from django.shortcuts import render

# Create your views here.
from django.shortcuts import render, redirect
from django.views import View
from django import http
import re
from users.models import User
from django_redis import get_redis_connection
from django.contrib.auth import authenticate,login
# 1, 注册页面视图
class RegisterView(View):
    def get(self,request):
        return render(request,'register.html')

    def post(self,request):
        #1, 获取参数
        user_name = request.POST.get("user_name")
        pwd = request.POST.get("pwd")
        cpwd = request.POST.get("cpwd")
        phone = request.POST.get("phone")
        msg_code = request.POST.get("msg_code")
        allow = request.POST.get("allow")

        #2, 校验参数
        #2,1 为空校验
        if not all([user_name,pwd,cpwd,phone,msg_code,allow]):
            return http.HttpResponseForbidden("参数不全")

        #2,2 两次密码一致
        if pwd != cpwd:
            return http.HttpResponseForbidden("两次密码不一致")

        #2,3 手机号格式正确
        if not re.match(r'^1[3-9]\d{9}$',phone):
            return http.HttpResponseForbidden("手机号格式有误")

        # 2,5 短信验证码正确
        redis_conn = get_redis_connection("code")
        redis_sms_code = redis_conn.get("sms_code_%s" % phone)
        if msg_code != redis_sms_code.decode():
            return http.HttpResponseForbidden("短信验证码填写错误")

        #2,6 协议需要同意
        if allow != 'on':
            return http.HttpResponseForbidden("协议需要同意")

        #3, 数据入库 (create_user可以对密码加密)
        user = User.objects.create_user(username=user_name,password=pwd,mobile=phone)

        #4, 返回响应
        return redirect('/')


#2, 判断用户名是否存在
class CheckUsernameView(View):
    def get(self,request,username):
        #1, 根据用户名查询用户数量
        count = User.objects.filter(username=username).count()

        #2, 返回响应
        return http.JsonResponse({"count":count,"code":0})
#3, 判断手机号是否存在
class CheckMobileView(View):
    def get(self,request,mobile):
        #1, 根据手机号查询用户数量
        count = User.objects.filter(mobile=mobile).count()

        #2, 返回响应
        return http.JsonResponse({"count":count,"code":0})



#4, 登录视图
class LoginView(View):
    def get(self,request):
        return render(request,'login.html')

    def post(self,request):
        #1,获取参数
        username = request.POST.get("username")
        pwd = request.POST.get("pwd")
        remembered = request.POST.get("remembered")

        #2,校验参数
        #2,1 为空校验
        if not all([username,pwd]):
            return http.HttpResponseForbidden("参数不全")

        #2,2 用户名格式
        if not re.match(r'^[a-zA-Z0-9_-]{5,20}$',username):
            return http.HttpResponseForbidden("用户名格式不对")

        #2,3 密码格式校验
        if not re.match(r'^[0-9A-Za-z]{8,20}$',pwd):
            return http.HttpResponseForbidden("密码格式不对")

        #2,4 账号密码正确性校验, 如果认证成功返回用户对象, 不成功返回None
        user = authenticate(request, username=username, password=pwd)

        if not user:
            return http.HttpResponseForbidden("账号或者密码不正确")

        #3,数据入库(状态保持)
        login(request, user) # 就是将用户的信息存储在session
        if remembered == "on":  #如果用户在前端选择记住密码
            request.session.set_expiry(3600*24*2) #保存两天
        else:
            request.session.set_expiry(0) #一次浏览器会话结束

        #4,返回响应
        return redirect("/")